<?php

namespace base;

use Yii;
use common\models\LoginForm;
use yii\data\Pagination;
use yii\helpers\VarDumper;
use yii\helpers\Url;

/**
 * Site controller
 */
class YiiMobo
{
	
	public static function createUrl($route){
		return Yii::$app->urlManager->createUrl($route);
	}
	
	public static function csrfToken($input = true){
		return $input ? '<input type="hidden" class="input-text" id="_csrf" name="_csrf" value="'.Yii::$app->request->csrfToken.'">' : Yii::$app->request->csrfToken;
	}
	
	
	public static function pageRows($sql, $order = '', $pageparam = []){
		$sqlcount = preg_replace("/^SELECT(.*)FROM/i", "SELECT count(*) FROM", $sql);
		$pages = new Pagination(array_merge([
            'totalCount'=> Yii::$app->db->createCommand($sqlcount)->queryScalar(),
			'PageSize' => 20,
        ], $pageparam));
        $models = Yii::$app->db->createCommand($sql. ' ' .$order." limit ".$pages->limit." offset ".$pages->offset."")->queryAll();
		return [
		    'rows' => $models,
            'pages' => $pages,
		];
	}
	
	

}
